﻿using NAudio.Wave;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LaunchAllaround.Sound
{
    public class WaveStream : WaveOut
    {

        public WaveStream(Song song)
        {
            this.song = song;
        }

        private Song song;
        public Song Song
        {
            get { return this.song; }
            set { this.song = value; }
        }

        private void Reset()
        {
            this.CloseTarget();
            this.target = new AudioFileReader(song.SongPath);
            this.target.Volume = this.song.Volume/100;
        }

        private AudioFileReader target;
        public void Play(Song song)
        {
            
                this.Song = song;
                this.Reset();
                this.Init(this.target);
                base.Play();
            
        }

        public new void Play()
        {
            if (this.song != null && SongPlayer.myself.IsAValidSong(this.song.SongPath))
            {
                try
                {
                    this.Reset();
                    this.Init(this.target);
                    base.Play();
                }
                catch (Exception e)
                {
                }
            }
        }

        public void CloseTarget()
        {
            if (this.target != null)
            {
                this.target.Dispose();
                this.target = null;
            }
        }
    }
}
